
	$(document).ready(function(){
		showAllBook();
			
	});
	
	function classBook(booklist){
		this.list=booklist;
		this.setList=function(booklist){
			this.list=booklist;
		};
		
		this.show=function(){
			var target=$("#book");
			target.empty();
			for(var i=0;i<this.list.length;i++){
			$("<tr id="+this.list[i].bookid+"><td>"+this.list[i].bookid+"</td><td>"+
					this.list[i].bookname+"</td><td>"+this.list[i].author+"</td><td>"+
					this.list[i].price+"</td><td><button onclick=\"deletebook('"+this.list[i].bookid+"')\">Delete</button></td>" +
			  		"<td><button onclick=\"updatebook('"+this.list[i].bookid+"')\" data-toggle='modal' data-target='#myModal'>Update</button></td></tr>").appendTo(target);
			}
		};
	}
	books=new classBook([]);
	
	function showAllBook(){
		$.ajax({
			"url":"/springMVCBookms20170807/book/showAllBook",
			"type":"get",
			"success":function(data){
				books.setList(data);
				books.show();
			},
			"error":function(){
				console.log("error");
			}
		});
	}
	
	
	function deletebook(bookid){
		console.log(bookid);
		if(confirm("确定删除吗")){
			$.ajax({
				"url":"/springMVCBookms20170807/book/"+bookid,
				"type":"delete",
				"success":function(data){
					showAllBook();
				}
				
			});
		}
		else{
			showAllBook();
		}
		
	}
	
	function updatebook(bookid){
		var h="#"+bookid;
		var id=$(h).children().get(0).innerHTML;
		var bookname=$(h).children().get(1).innerHTML;
		var author=$(h).children().get(2).innerHTML;
		var price=$(h).children().get(3).innerHTML;
		$("#bookid").val(id);
		$("#bookname").val(bookname);
		$("#author").val(author);
		$("#price").val(price);
		$("#updatebook").click();		
	}
	
	
	$("#updatebook").click(function(){
		$.ajax({
			"url":"/springMVCBookms20170807/book/"+bookid,
			"type":"put",
			"data":{
				"bookid":$("#bookid").val(),
				"bookname":$("#bookname").val(),
				"author":$("#author").val(),
				"price":$("#price").val()
			},
			"success":function(data){
				showAllBook();
			}
			
		});
		var modal=document.getElementById("updatebook");
		modal.setAttribute("data-dismiss","modal");
		
	});
	
	function addBook(){
		$("#addbook").click();
		$(".modal-body>input").val("");
	}
	
	$("#addbook").click(function(){
		$.ajax({
			"url":"/springMVCBookms20170807/book/add",
			"type":"post",
			"data":{
				"bookid":$("#addbookid").val(),
				"bookname":$("#addbookname").val(),
				"author":$("#addauthor").val(),
				"price":$("#addprice").val()
			},
			"success":function(data){
				showAllBook();
			}
			
		});
		var modal=document.getElementById("addbook");
		modal.setAttribute("data-dismiss","modal");
		
	});